package com.example.module_maintenance

import android.content.Intent
import androidx.lifecycle.ViewModelProvider

import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_base.path.RouterPath
import com.bw.lib_base.view.BaseActivity
import com.example.module_maintenance.databinding.ActivityAddcultureBinding
import com.example.module_maintenance.viewmodel.AddNewsViewModel
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
@Route(path = RouterPath.PATH_ADD_CULTURE_ACTIVITY)
class AddcultureActivity : BaseActivity<ActivityAddcultureBinding>(){

    private val vm by lazy { ViewModelProvider(this)[AddNewsViewModel::class.java] }

    override fun initView() {

        vm.addCultureSuccess.observe(this){
            ToastUtils.showLong("发布成功")
            startActivity(Intent(this,CultureActivity::class.java))
        }

        mDataBinding.back.setOnClickListener {
            onBackPressed()
        }

        mDataBinding.bottomFb.setOnClickListener {
            val whtitle = mDataBinding.etWhTitle.text.toString()
            val whtype = mDataBinding.etWhType.text.toString()
            val whperson = mDataBinding.etWhPerson.text.toString()
            val whcontent = mDataBinding.etWhContent.text.toString()
            val whdate = mDataBinding.etWhDate.text.toString()

            val map = mutableMapOf<String,String>()
            map[RouterPath.TITLTE] = whtitle
            map[RouterPath.TP] = whtype
            map[RouterPath.PUBLISHER] = whperson
            map[RouterPath.CONTENT] = whcontent
            map[RouterPath.PUBLISHDATE]=whdate

            val json = GsonUtils.toJson(map)
            val body = RequestBody.create(RouterPath.TYPE.toMediaTypeOrNull(),json)
            vm.addCulture(body)

        }

    }









    override fun getLayoutId(): Int  = R.layout.activity_addculture

}